package com.xzt.meeting.service;

import com.xzt.meeting.dao.MeetingDao;
import com.xzt.meeting.entity.Meeting;
import com.xzt.meeting.entity.PageEntity;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author xzt
 * @Date 2022/5/8 21:24
 * @Version 1.0
 * @Introduce
 */
public class MeetingService {

    private MeetingDao meetingDao = new MeetingDao();

    public PageEntity<Meeting> findPage(Integer currPage, String title, Integer status) {

        PageEntity<Meeting> pageEntity = new PageEntity<>();

        Integer pageSize = pageEntity.getPageSize();

        Integer startIndex = (currPage-1) * pageSize;

        List<Meeting> meetingList = meetingDao.findPage(title,status,startIndex,pageSize);

        Integer totalCount = meetingDao.countBytitleAndstatus(title,status);

        Integer totalPage = totalCount % pageSize == 0 ? totalCount/pageSize:(totalCount/pageSize) + 1;

        pageEntity.setCurrPage(currPage);
        pageEntity.setData(meetingList);
        pageEntity.setTotalPage(totalPage);
        pageEntity.setPageSize(pageSize);
        pageEntity.setTotalCount(totalCount);
        return pageEntity;
    }

    public void save(Meeting meeting) {
        meeting.setStatus(0L);          //默认状态为0,未开始
        meeting.setPublishDate(new Date());     //发布日期为当前时间
        meetingDao.save(meeting);
    }

    public Meeting findMeetingById(Long meetingId) {
        return meetingDao.findMeetingById(meetingId);
    }

    public Integer countJoinMeetingByMeetingId(Long meetingId) {
        return meetingDao.countJoinMeetingByMeetingId(meetingId);
    }

    public Boolean isJoinMeeting(Long uid, Long meetingId) {
        Integer count = meetingDao.isJoinMeeting(uid,meetingId);
        return count == 0 ? false:true;
    }

    public void unJoinMeeting(Long uid, Long mid) {
        meetingDao.unJoinMeeting(uid, mid);
    }

    public void joinMeeting(Long uid, Long mid) {
        meetingDao.joinMeeting(uid, mid);
    }
}
